package com.jt.tenant.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.fragment.CarFragment;
import com.jt.tenant.fragment.HomeFragment;
import com.jt.tenant.fragment.MeFragment;
import com.jt.tenant.fragment.OrderFragment;
import com.jt.tenant.util.UtilPermission;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.zcx.helper.util.UtilToast;

import java.util.Calendar;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by Administrator on 2016/1/30.
 */
public class MainActivity extends JTV4Activity implements View.OnClickListener{

    private TextView title;

    private View home, car, order, me;

    private LinearLayout lin_title;

    private HomeFragment homeFragment = new HomeFragment();

    private CarFragment carFragment = new CarFragment();

    private OrderFragment orderFragment = new OrderFragment();

    private MeFragment meFragment = new MeFragment();

    private FragmentManager fragmentManager;

//    private TipDialog tipDialog;

    private Boolean isExit = false;

    public static MapLinstener mapLinstener;

    private TextView tv_main_home, tv_main_car, tv_main_order, tv_main_me;

    private ImageView iv_main_home, iv_main_car, iv_main_order, iv_main_me;

    private int flag;

    private String ins_flag;

    private Calendar calendar = Calendar.getInstance();

    private Integer month, day;

    private String text_on = "#ff6565", text_off = "#4b4b4b";

    private int home_off = R.mipmap.main_home_off;

    private int home_on = R.mipmap.main_home_on;

    private int car_off = R.mipmap.main_car_off;

    private int car_on = R.mipmap.main_car_on;

    private int order_off = R.mipmap.main_order_off;

    private int order_on = R.mipmap.main_order_on;

    private int me_off = R.mipmap.main_me_off;

    private int me_on = R.mipmap.main_me_on;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        lin_title = (LinearLayout) findViewById(R.id.lin_main_title);

        calendar = Calendar.getInstance();

        month = calendar.get(Calendar.MONTH) + 1;

        day = calendar.get(Calendar.DAY_OF_MONTH);


        if (month == 12 && 24 <= day && day <= 26) {



        } else if ((month == 12 && day == 31) || (month == 1 && 1 <= day && day <= 3)) {



        } else if ((month == 1 && 27 <= day && day <= 31) || (month == 2 && 1 <= day && day <= 3)) {



        }

        flag = getIntent().getIntExtra("id", 1);

//        JTApplication.jtPreferences.saveInsFlag("0");

        ins_flag = JTApplication.jtPreferences.readInsFlag();

//        ins_flag = "6";

        fragmentManager = getSupportFragmentManager();

            fragmentManager.beginTransaction()
                    .add(R.id.main_content, homeFragment)
                    .add(R.id.main_content, carFragment)
                    .add(R.id.main_content, orderFragment)
                    .add(R.id.main_content, meFragment)
                    .commitAllowingStateLoss();


//        .add(R.id.main_content, insuranceFragment)   .add(R.id.main_content, maintenanceFragment)

        (title = (TextView) findViewById(R.id.tv_title_name)).setText("简途");

        (home = findViewById(R.id.lin_main_home)).setOnClickListener(this);

        (car = findViewById(R.id.lin_main_car)).setOnClickListener(this);

        (order = findViewById(R.id.lin_main_order)).setOnClickListener(this);

        (me = findViewById(R.id.lin_main_me)).setOnClickListener(this);

        tv_main_home = (TextView) findViewById(R.id.tv_main_home);

        tv_main_car = (TextView) findViewById(R.id.tv_main_car);

        tv_main_order = (TextView) findViewById(R.id.tv_main_order);

        tv_main_me = (TextView) findViewById(R.id.tv_main_me);

        iv_main_home = (ImageView) findViewById(R.id.iv_main_home);

        iv_main_car = (ImageView) findViewById(R.id.iv_main_car);

        iv_main_order = (ImageView) findViewById(R.id.iv_main_order);

        iv_main_me = (ImageView) findViewById(R.id.iv_main_me);

        if (flag == 1) {

            onClick(home);

        } else if (flag == 2) {

            onClick(car);

        } else if (flag == 3) {

            onClick(order);

        } else if (flag == 4) {

            onClick(me);

        }

        AndPermission.with(MainActivity.this)
                .requestCode(200)
                .permission(Manifest.permission.INTERNET,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.ACCESS_WIFI_STATE,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                        Manifest.permission.CHANGE_WIFI_STATE,
                        Manifest.permission.CAMERA)
                .callback(mPermissionListener)
                .start();

    }

    private PermissionListener mPermissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantedPermissions) {
            // 权限申请成功回调。

            // 这里的requestCode就是申请时设置的requestCode。
            // 和onActivityResult()的requestCode一样，用来区分多个不同的请求。
            if(requestCode == 200) {
                // TODO ...
            }
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            // 权限申请失败回调。
            if(requestCode == 200) {
                // TODO ...
            }
        }
    };





    @Override
    public void onClick(View v) {

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();



        switch (v.getId()) {
            //主页
            case R.id.lin_main_home:

                initialize(fragmentTransaction);

                lin_title.setVisibility(View.GONE);

                iv_main_home.setImageResource(home_on);

                tv_main_home.setTextColor(Color.parseColor(text_on));

                title.setText("简途");

                fragmentTransaction.show(homeFragment);

                break;

            //爱车
            case R.id.lin_main_car:


                if(JTApplication.jtPreferences.readAccountState().equals("0")) {

                    UtilToast.show(context,"请先绑定车辆");


                }else {

                    initialize(fragmentTransaction);

                    iv_main_car.setImageResource(car_on);

                    tv_main_car.setTextColor(Color.parseColor(text_on));

                    title.setText("管家");

                    fragmentTransaction.show(carFragment);

                }

                break;

            //订单
            case R.id.lin_main_order:

                initialize(fragmentTransaction);

                iv_main_order.setImageResource(order_on);

                tv_main_order.setTextColor(Color.parseColor(text_on));

                title.setText("订单");

                fragmentTransaction.show(orderFragment);

                break;

            //我的
            case R.id.lin_main_me:

                initialize(fragmentTransaction);

                iv_main_me.setImageResource(me_on);

                tv_main_me.setTextColor(Color.parseColor(text_on));

                title.setText("我的");

                fragmentTransaction.show(meFragment);

                break;

        }

        fragmentTransaction.commitAllowingStateLoss();

    }

    private void hideFragment(FragmentTransaction fragmentTransaction, Fragment fragment) {

        if (fragment != null) {

            fragmentTransaction.hide(fragment);

        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }


//

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (HomeFragment.onDismiss == null || !HomeFragment.onDismiss.onDismiss()) {
//
//            if (keyCode == KeyEvent.KEYCODE_BACK) {
//
//                if (isExit == false) {
//
//                    isExit = true;
//
//                    Toast.makeText(MainActivity.this, "再按一次退出!", Toast.LENGTH_SHORT).show();
//
//                    Handler mHandler = new Handler() {
//                        @Override
//                        public void handleMessage(Message msg) {
//                            super.handleMessage(msg);
//                            isExit = false;
//
//                        }
//                    };
//                    mHandler.sendEmptyMessageDelayed(0, 3000);
//
//                    return true;
//
//                } else {
//
//                    JTApplication.INSTANCE.AppExit(context);
//
//                    return false;
//
//                }
//
//            }
//
//        }
//
//        return true;
//    }

    public abstract class MapLinstener {

        public abstract void mapActivity();

    }

    private void initialize(FragmentTransaction fragmentTransaction) {

        lin_title.setVisibility(View.VISIBLE);

        hideFragment(fragmentTransaction, homeFragment);

        hideFragment(fragmentTransaction, carFragment);

        hideFragment(fragmentTransaction, orderFragment);

        hideFragment(fragmentTransaction, meFragment);

        iv_main_home.setImageResource(home_off);

        iv_main_car.setImageResource(car_off);

        iv_main_order.setImageResource(order_off);

        iv_main_me.setImageResource(me_off);

        tv_main_home.setTextColor(Color.parseColor(text_off));

        tv_main_car.setTextColor(Color.parseColor(text_off));

        tv_main_order.setTextColor(Color.parseColor(text_off));

        tv_main_me.setTextColor(Color.parseColor(text_off));

    }

}
